using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MY_FSM
{
    //定义状态机的状态
    public enum StateType
    {
        Idle,//待机
        Move,//移动
        Find_Enemy,//寻找敌人
        Attack,//攻击
        Die,//死亡
        Success,//胜利
    }
    [Serializable]
    public class Blackboard
    {
    }
    //定义状态机接口
    public interface IState
    {
        void OnEnter();//进入状态
        void OnExit();//退出状态
        void OnUpdate();//状态更新
        // void OnCheck();//状态检查
        // void OnFixUpdate();//状态修正
    }
    
    public class FSM
    {
        public IState curState; //当前状态
        public Dictionary<StateType, IState> states;
        public Blackboard blackboard;

        public FSM(Blackboard board)
        {
            states = new Dictionary<StateType, IState>();
            blackboard = board;
        }
        
        public void AddState(StateType stateType, IState state){
            //判断状态是否存在
            if (states.ContainsKey(stateType))
            {
                Debug.Log("添加状态失败，状态已存在！"+ stateType);
                return;
            }
            states.Add(stateType, state);
        }
        
        public void SwitchState(StateType stateType)
        {
            //判断状态是否存在
            if (!states.ContainsKey(stateType))
            {
                Debug.Log("切换状态失败，状态不存在！" + stateType);
                return;
            }
            //退出当前状态
            if (curState!= null)
            {
                curState.OnExit();
            }
            //进入新状态
            curState = states[stateType];
            curState.OnEnter();
        }
        
        public void OnUpdate()
        {
            if (curState!= null)
            {
                curState.OnUpdate();
            }
        }   
        
        public void OnFixedUpdate()
        {
            
        }
        
        public void OnCheck()
        {
            Debug.Log("当前状态：" + curState.GetType().Name);
        }
    }   
}
